package isi_graf;
import isi_ib.barras_trade;
import isi_ib.barra_trade;
import java.util.ArrayList;
import java.util.Iterator;

import java.util.Calendar;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class GraficoBarras extends ApplicationFrame {
	
	public GraficoBarras(String titel,barras_trade barras_) {
		
	super(titel);
	
	//barras_.cargaSql(date);
	final DefaultHighLowDataset dataset = createDataset(barras_);
	final JFreeChart chart = createChart(dataset);
	chart.getXYPlot().getRangeAxis().setRange(barras_.minvalue, barras_.maxvalue);
	final ChartPanel chartPanel = new ChartPanel(chart);
	chartPanel.setPreferredSize(new java.awt.Dimension(600, 350));
	
	
	setContentPane(chartPanel);
	}
	
	private Date createData(int year, int month, int date) {
		  Calendar calendar = Calendar.getInstance();
		  calendar.set(year, month - 1, date);
		  return calendar.getTime();
		  }
    private JFreeChart createChart(final 
		  DefaultHighLowDataset dataset) {
		  final JFreeChart chart = ChartFactory.createCandlestickChart(
		  "Candlestick Demo", "Time", "Price", dataset, false);
		  //chart.getPlot()
		  return chart;
		  }
    private DefaultHighLowDataset createDataset(barras_trade barras_) {

		 
		  Date[] date = new Date[barras_.count()];
		  double[] high = new double[barras_.count()];
		  double[] low = new double[barras_.count()];
		  double[] open = new double[barras_.count()];
		  double[] close = new double[barras_.count()];
		  double[] volume = new double[barras_.count()];



		  //Calendar calendar = Calendar.getInstance();
		  //calendar.set(2008, 5, 1);
		  
		  Iterator<barra_trade> itr = barras_.barras.iterator();
		  int i=0;
		    while (itr.hasNext()) {
		      barra_trade element = itr.next();
		      date[i] =element.getDate();
		      high[i] = element.getHigh();
		      low[i] = element.getLow();
		      open[i] = element.getOpen();
		      close[i] = element.getClose();
		      volume[i]=element.getVolume();
		      i++;
		    }
		 
		  DefaultHighLowDataset data = new DefaultHighLowDataset(
		  "", date, high, low, open, close, volume);
		  return data;
		  }
    private double getLowestLow(DefaultHighLowDataset dataset){
        double lowest;
        lowest = dataset.getLowValue(0,0);
        for(int i=1;i<dataset.getItemCount(0);i++){
            if(dataset.getLowValue(0,i) < lowest){
                lowest = dataset.getLowValue(0,i);
            }
        }

        return lowest;
    }


    private double getHighestHigh(DefaultHighLowDataset dataset){
        double highest;
        highest = dataset.getHighValue(0,0);
        for(int i=1;i<dataset.getItemCount(0);i++){
            if(dataset.getLowValue(0,i) > highest){
                highest = dataset.getHighValue(0,i);
            }
        }

        return highest;
    }   
}
